package com.sgb.kjwl.viewmodel.manage

import android.app.Activity
import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.model.entity.LoginEntity
import com.swgk.core.base.api.ICallBack
import com.sgb.kjwl.model.entity.manage.AgentStaffCheckEntity
import com.sgb.kjwl.model.response.ListMResponse
import com.sgb.kjwl.view.event.RoleListEntity
import com.swgk.core.base.model.entity.ListResponse
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.sgb.member.MemberConfig
import com.sgb.member.entity.MemberAuditBean
import com.sgb.member.entity.MemberBean
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.H5ReEntity
import com.swgk.core.dialog.DialogHelper
import com.swgk.core.util.MToast

class AgentCheckModel : BaseViewModel() {

    private var page = 1 //第几页
    private var limit = 10 //每页加载个数
    private val map = HashMap<String, Any>()

    var checkDatas = MutableLiveData<ArrayList<MemberAuditBean>>()

    var staffCheckList = MutableLiveData<ArrayList<AgentStaffCheckEntity>>()

    var roleList = MutableLiveData<ArrayList<RoleListEntity>>()


    var rejectSuccess = MutableLiveData<String>()

    var addStaffSuccess = MutableLiveData<String>()

    //im信息
    val mImMsg = MutableLiveData<LoginEntity>()

    /**
     * 审核列表
     * @param isLoadMore
     * @param type
     */
    fun getMixAuditingMemberByPage(context: Context, isLoadMore: Boolean, type: String) {
        page = if (isLoadMore) page + 1 else 1
        DialogHelper.showProgressDialog(context, "数据获取中...").setCanceledOnTouchOutside(false)
        val map: MutableMap<String, Any> = java.util.HashMap()
        map["limit"] = limit
        map["page"] = page
        map["joinWay"] = "" // 加盟方式:1申请 2邀请
        map["memberType"] = "" // 会员类型:1企业 2个人
        map["relation"] = "" // 供需关系1供方,2需方
        map["status"] = "1" // 状态:1待审批,2审批中,3审批通过
        APIManager.getInstance().userAPI.getMixAuditingMemberByPage(map).enqueue(object : ICallBack<BaseEntity<ListMResponse<List<MemberAuditBean>>>>(){
            override fun onSuccess(data: BaseEntity<ListMResponse<List<MemberAuditBean>>>?) {
                DialogHelper.dismissProgressDialog()
                if (data != null && data.data != null && data.data.list != null){
                    checkDatas.postValue(data.data.list as ArrayList<MemberAuditBean>)
                }
            }
        })

    }

    //获取h5链接
    fun getUrlSetting(context: Context, param: String?, endUrl: String) {
        DialogHelper.showProgressDialog(context, "数据加载中...").setCanceledOnTouchOutside(false)
        APIManager.getInstance().pickAPI.getUrlSetting(param)
            .enqueue(object : ICallBack<BaseEntity<H5ReEntity>>() {
                override fun onSuccess(data: BaseEntity<H5ReEntity>) {
                    val url = data.data.url + endUrl
                    WebViewX5HostActivity.start(context, url, 101)
                }
            })
    }

    //获取员工审核列表
    fun getStaffCheckList(context: Context, isLoadMore: Boolean){
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        page = if (isLoadMore) page + 1 else 1
        APIManager.getInstance().userAPI.getStaffCheckList("".plus(page), "".plus(limit)).enqueue(object : ICallBack<BaseEntity<ListResponse<List<AgentStaffCheckEntity>>>>(){
            override fun onSuccess(data: BaseEntity<ListResponse<List<AgentStaffCheckEntity>>>?) {
                DialogHelper.dismissProgressDialog()
                if (data != null && data.data != null && data.data.list != null){
                    staffCheckList.postValue(data.data.list as ArrayList<AgentStaffCheckEntity>)
                }
            }
        })
    }

    //获取角色列表
    fun getRoleList(context: Context, companyNo : String){
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().userAPI.getRole().enqueue(object : ICallBack<BaseEntity<List<RoleListEntity>>>(){
            override fun onSuccess(data: BaseEntity<List<RoleListEntity>>?) {
                DialogHelper.dismissProgressDialog()
                if (data != null && data.data != null){
                    roleList.postValue(data.data as ArrayList<RoleListEntity>)
                }
            }

        })

    }

    //拒绝加入
    /**
     * reason 拒绝理由
     * checkId 审核id
     */
    fun rejectAdd(context: Context, reason : String, checkId : String){
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null)
            ?.setCanceledOnTouchOutside(false)
        map["id"] = checkId
        map["rejectMark"] = reason
        APIManager.getInstance().userAPI.rejectAdd(map).enqueue(object : ICallBack<BaseEntity<*>>(){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null){
                   rejectSuccess.postValue("操作成功")
                }
            }
        })
    }

    //添加成功
    fun addStaff(context: Context, entity : AgentStaffCheckEntity){
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null)
            ?.setCanceledOnTouchOutside(false)
        APIManager.getInstance().userAPI.addStaff(entity).enqueue(object : ICallBack<BaseEntity<*>>(){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null){
                    addStaffSuccess.postValue("成员添加成功")
                }
            }
        })
    }

    /**
     * 获取im账号
     *
     */
    fun getImAccount(siteNo : String, siteType : String){
        APIManager.getInstance().websiteInfoAPI.getImAccount(siteNo, siteType).enqueue(object : ICallBack<BaseEntity<LoginEntity>>(true, true){
            override fun onSuccess(data: BaseEntity<LoginEntity>?) {
                if (data != null && data.data != null){
                    mImMsg.postValue(data.data)
                }
            }
        })
    }

}